package com.yusong.algorithm.link;


/**
 给定一个 排序 链表，删除所有含有重复数字的节点，只保留原始链表中 没有重复出现 的数字。
 */
public class DeleteDuplicates82 {

    public static void main(String[] args) {
        int[] data = {1,2,3,3,4,4,5};
        ListNode node = ListNodeUtil.create(data);

        DeleteDuplicates82 demo = new DeleteDuplicates82();
        node = demo.deleteDuplicates(node);
        ListNodeUtil.printNode(node);
    }

    /**
     * 快慢指针
     * 快指针在循环内部直接系统
     */
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }

        //不可能取值
        ListNode dummy = new ListNode(Integer.MAX_VALUE);
        dummy.next = head;
        //当前比较值的前一个
        ListNode slow = dummy;
        ListNode fast = head.next;

        while (fast != null){
            int slowVal = slow.next.val;
            int fastVal = fast.val;
            if(slowVal == fastVal){
                while (fastVal == slowVal){
                    fast = fast.next;
                    if(fast == null){
                        fastVal = Integer.MIN_VALUE;
                    }else {
                        fastVal = fast.val;
                    }
                }
                slow.next = fast;
                //slow = slow.next;
                if(fast != null){
                    fast = fast.next;
                }
            }else {
                slow = slow.next;
                fast = fast.next;
            }


        }
        return dummy.next;
    }



    public ListNode deleteDuplicatesOffical(ListNode head) {
        if(head==null || head.next==null) {
            return head;
        }
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode a = dummy;
        ListNode b = head;
        while(b!=null && b.next!=null) {
            //初始化的时a指向的是哑结点，所以比较逻辑应该是a的下一个节点和b的下一个节点
            if(a.next.val!=b.next.val) {
                a = a.next;
                b = b.next;
            }
            else {
                //如果a、b指向的节点值相等，就不断移动b，直到a、b指向的值不相等
                while(b!=null && b.next!=null && a.next.val==b.next.val) {
                    b = b.next;
                }
                a.next = b.next;
                b = b.next;
            }
        }
        return dummy.next;
    }


    /*
    将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
    */
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(null == l1){
            return l2;
        }
        if(null == l2){
            return l1;
        }
        ListNode dummy = new ListNode(Integer.MAX_VALUE);
        ListNode cur = dummy;
        while (l1 != null || l2 != null){
            if(l1 == null){
                cur.next = l2;
                break;
            }
            if(l2 == null){
                cur.next = l1;
                break;
            }
            int val1 = l1.val;
            int val2 = l2.val;
            if(val1 < val2){
                cur.next = l1;
                l1 = l1.next;
            }else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }

        return dummy.next;
    }

}


/*

输入: 1,2,3,3,4,4,5
输出: 1,2,5
 */